package com.bw.day03.search

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.SearchView
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.SPUtils
import com.bw.base.net.mvvm.BaseActivity
import com.bw.day03.R
import com.bw.day03.adapter.GoodsAdapter
import com.bw.day03.databinding.ActivitySearchBinding
import com.bw.day03.room.AppDBUtlis
import com.bw.day03.room.Record
import com.google.android.material.chip.Chip
import java.text.SimpleDateFormat

@Route(path = "/app/SearchActivity")
class SearchActivity : BaseActivity<ActivitySearchBinding,SearchViewModel>() {
    lateinit var goodsAdapter: GoodsAdapter
    override fun initView() {
        goodsAdapter=GoodsAdapter()
        binding.rvGoods.adapter=goodsAdapter
        binding.rvGoods.layoutManager=GridLayoutManager(this,2)
        binding.search.setOnQueryTextListener(object :SearchView.OnQueryTextListener{
            override fun onQueryTextSubmit(query: String?): Boolean {
                //点击回车键触发
                if (query!=null){
//                    Log.d("ssss", "onQueryTextSubmit: $query")
                    viewModel.search(query,10,1)
                    var simpleDateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                    var currentTimeMillis = System.currentTimeMillis()
                    var format = simpleDateFormat.format(currentTimeMillis)
                    AppDBUtlis.getDB().getRecordDao().insert(Record(query,format,SPUtils.getInstance().getString("phone")))
                    queryRecord()
                }

                return false
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                //文字发生变化后触发
                if (newText!=null){
                    viewModel.search(newText,10,1)
                }
                return false
            }
        })

        queryRecord()
        binding.delete.setOnClickListener {
            AppDBUtlis.getDB().getRecordDao().deleteAll(SPUtils.getInstance().getString("phone"))
            binding.cgRecord.removeAllViews()
        }
    }

    private fun queryRecord() {
        var queryAll =
            AppDBUtlis.getDB().getRecordDao().queryAll(SPUtils.getInstance().getString("phone"))
        Log.d("ssss", "initView: ${queryAll.size}")
        queryAll.reverse()
        if (queryAll.isNotEmpty()){
            binding.cgRecord.removeAllViews()
            queryAll.forEach {
                val chip=LayoutInflater.from(this).inflate(R.layout.item_record,null) as Chip
                chip.findViewById<TextView>(R.id.name).text=it.keyword
                binding.cgRecord.addView(chip)
                chip.setOnClickListener {it1->
                    binding.search.setQuery(it.keyword,true)
                }
                chip.setOnCloseIconClickListener { it2->
                    AppDBUtlis.getDB().getRecordDao().delete(it)
                    binding.cgRecord.removeView(it2)
                }
            }
        }
    }

    override fun initData() {
        viewModel.searchSuccess.observe(this){
            if (it.code==200){
                goodsAdapter.data.clear()
                goodsAdapter.data.addAll(it.data.list)
                goodsAdapter.notifyDataSetChanged()
            }
        }
    }

    override fun getViewModel(): Class<SearchViewModel> =SearchViewModel::class.java

    override fun getLayoutId(): Int =R.layout.activity_search

}